package com.cloud.shopping.page.feign.controller;

import com.cloud.shopping.page.feign.service.SeckillPageService;
import com.cloud.shopping.seckill.feign.SeckillGoodsFeign;
import com.cloud.shopping.seckill.model.SeckillGoods;
import com.cloud.shopping.tool.RespResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author John.zhang
 * @version Id: SeckillPageController, v 1.0 2021/1/8 11:50 ZhiYong Exp $
 */

@RestController
@RequestMapping(value = "/page")
public class SeckillPageController {

    @Resource
    private SeckillPageService seckillPageService;

    @Resource
    private SeckillGoodsFeign seckillGoodsFeign;

    /**
     * 生成秒杀商品详情页
     *
     * @param id id
     * @return RespResult
     * @throws Exception Exception
     */
    @GetMapping(value = "/seckill/goods/{id}")
    public RespResult page(@PathVariable("id") String id) throws Exception {
        //生成秒杀商品详情页
        seckillPageService.html(id);
        return RespResult.ok();
    }


    /**
     * 删除指定活动的页面
     *
     * @param acid acid
     * @return RespResult
     */
    @DeleteMapping(value = "/seckill/delete/{acid}")
    public RespResult deleteByAct(@PathVariable("acid") String acid) {
        //1.查询当前活动ID对应的商品列表数据
        RespResult<List<SeckillGoods>> listRespResult = seckillGoodsFeign.actGoods(acid);
        List<SeckillGoods> goodsList = listRespResult.getData();
        //2.根据列表数据删除页面
        if (goodsList != null) {
            //循环删除页面
            for (SeckillGoods seckillGoods : goodsList) {
                seckillPageService.delete(seckillGoods.getId());
            }
        }
        return RespResult.ok();
    }

}
